home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995…tember: Reference Library / Dev.CD Sep 95 RL / Dev.CD Sep 95 RL.toast / mac / Technical Documentation / develop / develop Issue 19 code / SimpliFace_V2 / Sources / WindowObj.cp < prev    next >
Encoding:
Text File  |  1994-05-01  |  22.6 KB  |  1,155 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        WindowObj.h
  3.  
  4.     Contains:    Windows for a simple Scriptable application.
  5.  
  6.  
  7.     Developed by:
  8.  
  9.     Paul G Smith (commstalk hq & Full Moon Software, Inc)
  10.  
  11.     you can leave messages at (UK): 0727 844232; (US): 408 253 7199
  12.     BUT I prefer to be contacted by e-mail
  13.     AppleLink:     COMMSTALK.HQ
  14.     Internet:     COMMSTALK.HQ@applelink.apple.com
  15.  
  16.     "SimpliFace2" Sample code to accompany develop article
  17.     on techniques for controlling script inheritance.
  18.     
  19.     
  20.  
  21.  
  22. */
  23.  
  24.  
  25. #ifndef __STDIO__
  26. #include <StdIO.h>
  27. #endif
  28.  
  29. #ifndef __WINDOWOBJ__
  30. #include "WindowObj.h"
  31. #endif
  32.  
  33. #ifndef __SimpliFace2COMMON__
  34. #include "SimpliFace2Common.h"
  35. #endif
  36.  
  37. #ifndef __APPLICATIONCOMMON__
  38. #include "ApplicationCommon.h"
  39. #endif
  40.  
  41. #ifndef __MENUS__
  42. #include <Menus.h>
  43. #endif
  44. #ifndef __FONTS__
  45. #include <Fonts.h>
  46. #endif
  47. #ifndef __EVENTS__
  48. #include <Events.h>
  49. #endif
  50. #ifndef __WINDOWS__
  51. #include <Windows.h>
  52. #endif
  53. #ifndef __DIALOGS__
  54. #include <Dialogs.h>
  55. #endif
  56. #ifndef __QUICKDRAW__
  57. #include <Quickdraw.h>
  58. #endif
  59. #ifndef __MEMORY__
  60. #include <Memory.h>
  61. #endif
  62. #ifndef __RESOURCES__
  63. #include <Resources.h>
  64. #endif
  65. #ifndef __TOOLUTILS__
  66. #include <ToolUtils.h>
  67. #endif
  68. #ifndef __FILES__
  69. #include <Files.h>
  70. #endif
  71. #ifndef __STANDARDFILE__
  72. #include <StandardFile.h>
  73. #endif
  74. #ifndef __SYSEQU__
  75. #include <SysEqu.h>
  76. #endif
  77. #ifndef __PLSTRINGFUNCS__
  78. #include <PLStringFuncs.h>
  79. #endif
  80.  
  81. #ifndef __AEOBJECTS__
  82. #include <AEObjects.h>
  83. #endif
  84.  
  85. #ifndef __AERegistry__
  86. #include <AERegistry.h>
  87. #endif
  88. #ifndef __ASREGISTRY__
  89. #include <ASRegistry.h>
  90. #endif
  91.  
  92. #ifndef __AEOBJECTPACKING__
  93. #include <AEPackObject.h>
  94. #endif
  95.  
  96. #ifndef __AEOMTOKENS__
  97. #include "ObjModelTokens.h"
  98. #endif
  99. #ifndef __AEOMEVENTS__
  100. #include "ObjModelEvents.h"
  101. #endif
  102.  
  103. #ifndef __SCRIPTUTILS__
  104. #include "ScriptUtils.h"
  105. #endif
  106.  
  107.  
  108.  
  109. /**********************************************************************
  110. ** PUBLIC Constructor
  111. ***********************************************************************/
  112.  
  113. // when window object is first created, no window is opened;
  114. // (unless the window property 'visible' is initially true)
  115. // the window record is created and actually opened the first
  116. // time the window receives an 'open' event or is made 'visible'
  117.  
  118. TWindowObj::TWindowObj(TScriptableObject* curParent) 
  119.         : TScriptableObject(curParent)
  120. {    
  121.     InitLabels();
  122.     fInitialized = true;
  123. }
  124.  
  125. TWindowObj::TWindowObj(TScriptableObject* curParent, const AEDesc *theData) 
  126.         : TScriptableObject(curParent)
  127. {    
  128.     InitLabels();
  129.     SetData(theData);
  130.     fInitialized = true;
  131. }
  132.  
  133. TWindowObj::~TWindowObj()
  134. {
  135.     TListOfLongs    aList = fButtonElems;    // makes temporary copy of list
  136.     long             numElems = aList.CountElements();
  137.     while (numElems > 0)
  138.     {
  139.         delete (TInterfaceObj*)(aList.GetElement(numElems));
  140.         numElems--;
  141.     }
  142.     aList = fLabelElems;
  143.     numElems = aList.CountElements();
  144.     while (numElems > 0)
  145.     {
  146.         delete (TInterfaceObj*)(aList.GetElement(numElems));
  147.         numElems--;
  148.     }
  149.     fButtonElems.SetData(NULL);
  150.     fLabelElems.SetData(NULL);
  151.     
  152.     if (fWindowPtr)
  153.     {
  154.         CloseWindow(fWindowPtr);
  155.         fWindowPtr = NULL;
  156.     }
  157. }
  158.  
  159. void TWindowObj::InitLabels(void)
  160. {
  161.     fInitialized = false;
  162.     fBounds.left = 100;
  163.     fBounds.top = 100;
  164.     fBounds.right = 200;
  165.     fBounds.bottom = 200;
  166.     fHasCloseBox = true;
  167.     fHasTitleBar = true;
  168.     fIsModal = false;
  169.     fIsResizable = false;
  170.     fIsZoomable = false;
  171.     fIsZoomed = false;
  172.     fName = "";
  173.     fShouldBeVisible = false;
  174.     fHasBeenCreated = false;
  175.     fWindowDefProc = documentProc;
  176.     fWindowPtr = NULL;
  177.     fButtonElems.SetData(NULL);
  178.     fLabelElems.SetData(NULL);
  179. }
  180.  
  181.  
  182. void TWindowObj::ActivateWindow(Boolean isActivating)
  183. {
  184. }
  185.  
  186.  
  187. void TWindowObj::UpdateWindow(void)
  188. {
  189.     if (fWindowPtr)
  190.     {
  191.         SetPort(fWindowPtr);
  192.         BeginUpdate(fWindowPtr);
  193.         DrawWindow();
  194.         EndUpdate(fWindowPtr);
  195.     }
  196. }
  197.  
  198.  
  199. void TWindowObj::DrawAllElements(DescType desiredClass)
  200. {
  201.     if (fWindowPtr)
  202.     {
  203.         TListOfLongs    aList;    // makes temporary copy of list
  204.         
  205.         if (desiredClass == cButton)
  206.             aList = fButtonElems;
  207.         else if (desiredClass == cTextLabel)
  208.             aList = fLabelElems;
  209.             
  210.         long numElems = aList.CountElements();
  211.         while (numElems > 0)
  212.         {
  213.             TInterfaceObj*    anElement = (TInterfaceObj*)(aList.GetElement(numElems));
  214.             if (anElement)
  215.                 anElement->DrawObject();
  216.             numElems--;
  217.         }
  218.     }
  219. }
  220.  
  221.  
  222. void TWindowObj::DrawWindow(void)
  223. {
  224.     if (fWindowPtr)
  225.     {
  226.         DrawAllElements(cTextLabel);
  227.         DrawAllElements(cButton);
  228.     }
  229. }
  230.  
  231.  
  232. /**********************************************************************
  233. ** PUBLIC AE Object Model support
  234. ***********************************************************************/
  235.  
  236.  
  237. OSErr TWindowObj::CountElements(DescType desiredClass, long *result)
  238. {
  239.     OSErr             err = errAEEventNotHandled;
  240.  
  241.     if (desiredClass == cButton)
  242.     {
  243.         *result = fButtonElems.CountElements();
  244.         err = 0;
  245.     }    
  246.     else if (desiredClass == cTextLabel)
  247.     {
  248.         *result = fLabelElems.CountElements();
  249.         err = 0;
  250.     }    
  251.     return err;
  252. }
  253.  
  254.                                     
  255. OSErr TWindowObj::CreateNewElement    (DescType desiredClass,
  256.                                         DescType position,
  257.                                         AEDesc *theData,
  258.                                         AERecord *theProperties,
  259.                                         TScriptableObject *theContainerObj,
  260.                                         TScriptableObject **theNewObj)
  261. {
  262.     OSErr             err = errAEEventNotHandled;
  263.     TInterfaceObj    *newInterfaceObj = NULL;
  264.     
  265.     printf("TWindowObj::CreateNewElement: class='%.4s', thisObjScript=%ld\n",
  266.             &desiredClass, GetObjScript()); 
  267.         
  268.     if (desiredClass == cButton)
  269.     {
  270.         if (theProperties)
  271.             newInterfaceObj = new TButtonObj(this, theProperties);
  272.         else
  273.         {
  274.             newInterfaceObj = new TButtonObj(this);
  275.             if (newInterfaceObj && theData)
  276.                 newInterfaceObj->SetProperty(pName, theData);
  277.         }
  278.     }
  279.     else if (desiredClass == cTextLabel)
  280.     {
  281.         if (theProperties)
  282.             newInterfaceObj = new TLabelObj(this, theProperties);
  283.         else
  284.         {
  285.             newInterfaceObj = new TLabelObj(this);
  286.             if (newInterfaceObj && theData)
  287.                 newInterfaceObj->SetProperty(pName, theData);
  288.         }
  289.     }
  290.  
  291.     if (newInterfaceObj)
  292.     {
  293.         if (desiredClass == cButton)
  294.             fButtonElems.InsertElement((long)newInterfaceObj);
  295.         else
  296.             fLabelElems.InsertElement((long)newInterfaceObj);
  297.         *theNewObj = newInterfaceObj;
  298.         err = 0;
  299.     }
  300.  
  301.     return err;
  302. }    
  303.  
  304.  
  305. OSErr TWindowObj::ResolveContainer(TScriptableObject **theContainerObj)
  306. {
  307.     OSErr             err = 0;
  308.     
  309.     *theContainerObj = gSimpliFace2;
  310.  
  311.     return err;
  312. }
  313.  
  314.                                     
  315. OSErr TWindowObj::ResolveElementByName(DescType desiredClass,
  316.                                         CStr255& nameStr,
  317.                                         TScriptableObject **theResultObj)
  318. {
  319.     OSErr           err = errAEEventNotHandled;
  320.     TInterfaceObj    *anObj = NULL;
  321.     CStr255         aName;
  322.     TListOfLongs    aList;    // makes temporary copy of list
  323.     
  324.     if (desiredClass == cButton)
  325.         aList = fButtonElems;
  326.     else if (desiredClass == cTextLabel)
  327.         aList = fLabelElems;
  328.     else
  329.         return err;
  330.         
  331.     long numElems = aList.CountElements();
  332.     while (numElems > 0)
  333.     {
  334.         anObj = (TInterfaceObj*)(aList.GetElement(numElems));
  335.         if (anObj)
  336.         {
  337.             anObj->GetName(aName);
  338.             if (aName == nameStr)
  339.             {
  340.                 *theResultObj = anObj;
  341.                 numElems = 0;
  342.                 err = 0;
  343.             }
  344.         }
  345.         numElems--;
  346.     }
  347.     
  348.     return err;
  349. }
  350.  
  351.                                     
  352. OSErr TWindowObj::ResolveElementByIndex(DescType desiredClass,
  353.                                         short theIndex,
  354.                                         TScriptableObject **theResultObj)
  355. {
  356.     OSErr           err = errAEEventNotHandled;
  357.     TInterfaceObj    *anObj = NULL;
  358.     CStr255         aName;
  359.     TListOfLongs    aList;    // makes temporary copy of list
  360.     short            index = theIndex;
  361.     
  362.     if (desiredClass == cButton)
  363.         aList = fButtonElems;
  364.     else if (desiredClass == cTextLabel)
  365.         aList = fLabelElems;
  366.     else
  367.         return err;
  368.         
  369.     if (index<0)
  370.     {
  371.         short numElems = (short)aList.CountElements();
  372.         index = numElems+index+1;
  373.     }
  374.         
  375.     *theResultObj = (TInterfaceObj*)(aList.GetElement(index));
  376.     if (*theResultObj != NULL)
  377.         err = 0;
  378.     
  379.     return err;
  380. }
  381.  
  382.  
  383.  
  384. OSErr TWindowObj::OpenObject(void)
  385. {
  386.     OSErr                 err = 0;
  387.     Boolean             isVisible = false;
  388.     
  389.     if (fWindowPtr)
  390.         isVisible = ((WindowPeek)fWindowPtr)->visible;
  391.     
  392.     if (fInitialized && !isVisible)
  393.     {
  394.         fShouldBeVisible = true;
  395.         if (!fWindowPtr)
  396.         {
  397.             Rect        theBounds = fBounds;
  398.             CStr255        theName = fName;
  399.             
  400.             fWindowPtr = NewWindow(NULL, &theBounds, theName, 
  401.                                     false, fWindowDefProc, WindowPtr(-1),
  402.                                     fHasCloseBox, (long)this);
  403.             
  404.         }
  405.         if (fWindowPtr)
  406.         {
  407.             ShowWindow(fWindowPtr);
  408.         }
  409.     }
  410.  
  411.     printf("TWindowObj::OpenObject(): err = %d\n", err);
  412.     return err;
  413. }
  414.  
  415. OSErr TWindowObj::CloseObject(void)
  416. {
  417.     OSErr                 err = 0;
  418.     Boolean             isVisible = false;
  419.     
  420.     if (fWindowPtr)
  421.         isVisible = ((WindowPeek)fWindowPtr)->visible;
  422.     if (fInitialized && isVisible)
  423.     {
  424.         fShouldBeVisible = false;
  425.         if (fWindowPtr)
  426.         {
  427.             HideWindow(fWindowPtr);
  428.         }
  429.     }
  430.  
  431.     return err;
  432. }
  433.                                     
  434. OSErr TWindowObj::GetProperty  (DescType propertyID, DescType wantType, AEDesc *result)
  435. {
  436.     OSErr                 err = errAEEventNotHandled;
  437.     Boolean             theBoolean;
  438.     
  439.     switch (propertyID)
  440.     {
  441.     case pBounds:
  442.         Rect theRect = fBounds;
  443.         if (fWindowPtr)
  444.             theRect = (*((WindowPeek)fWindowPtr)->strucRgn)->rgnBBox;
  445.         err = AECreateDesc(typeQDRectangle, (Ptr)&theRect,
  446.                             sizeof(theRect), result);
  447.         break;
  448.     case pName:
  449.         {
  450.             CStr255        theName = fName;
  451.             err = AECreateDesc(typeChar, (Ptr)&theName[1], 
  452.                                 theName.Length(), result);
  453.         }
  454.         break;
  455.     case pHasCloseBox:
  456.         theBoolean = fHasCloseBox;
  457.         err = AECreateDesc(typeBoolean, (Ptr)&theBoolean,
  458.                             sizeof(theBoolean), result);
  459.         break;
  460.     case pHasTitleBar:
  461.         theBoolean = fHasTitleBar;
  462.         err = AECreateDesc(typeBoolean, (Ptr)&theBoolean,
  463.                             sizeof(theBoolean), result);
  464.         break;
  465.     case pIsModal:
  466.         theBoolean = fIsModal;
  467.         err = AECreateDesc(typeBoolean, (Ptr)&theBoolean,
  468.                             sizeof(theBoolean), result);
  469.         break;
  470.     case pIsResizable:
  471.         theBoolean = fIsResizable;
  472.         err = AECreateDesc(typeBoolean, (Ptr)&theBoolean,
  473.                             sizeof(theBoolean), result);
  474.         break;
  475.     case pIsZoomable:
  476.         theBoolean = fIsZoomable;
  477.         err = AECreateDesc(typeBoolean, (Ptr)&theBoolean,
  478.                             sizeof(theBoolean), result);
  479.         break;
  480.     case pIsZoomed:
  481.         theBoolean = fIsZoomed;
  482.         err = AECreateDesc(typeBoolean, (Ptr)&theBoolean,
  483.                             sizeof(theBoolean), result);
  484.         break;
  485.     case pIndex:
  486.         short theIndex = 0;
  487.         if (fWindowPtr)
  488.         {
  489.             do 
  490.                 theIndex++;
  491.             while (fWindowPtr != GetWindowPtrOfNthWindow(theIndex));
  492.         }
  493.         err = AECreateDesc(typeShortInteger, (Ptr)&theIndex,
  494.                             sizeof(theIndex), result);
  495.         break;
  496.     case pVisible:
  497.         theBoolean = false;
  498.         if (fWindowPtr)
  499.             theBoolean = ((WindowPeek)fWindowPtr)->visible;
  500.         err = AECreateDesc(typeBoolean, (Ptr)&theBoolean,
  501.                             sizeof(theBoolean), result);
  502.         break;
  503.     default:
  504.         err = TScriptableObject::GetProperty(propertyID, wantType, result);
  505.         break;
  506.     }
  507.     
  508.     return err;
  509. }
  510.                                             
  511. OSErr TWindowObj::SetProperty  (DescType propertyID, const AEDesc *theData)
  512. {
  513.     OSErr                 err = errAEEventNotHandled;
  514.     
  515.     if (fWindowPtr && (propertyID == pHasCloseBox ||
  516.                          propertyID == pHasTitleBar ||
  517.                          propertyID == pIsModal ||
  518.                          propertyID == pIsResizable ||
  519.                          propertyID == pIsZoomable))
  520.         return errAENotModifiable;
  521.         
  522.     switch (propertyID)
  523.     {
  524.     case pBounds:
  525.         {
  526.             Rect    newBounds;
  527.             err = GetRectFromDescriptor(theData, &newBounds);
  528.             if (err == noErr)
  529.             {
  530.                 fBounds = newBounds;
  531.                 if (fInitialized && fWindowPtr)
  532.                 {
  533.                     MoveWindow(fWindowPtr, newBounds.left, newBounds.top, false);
  534.                     SizeWindow(fWindowPtr, newBounds.right-newBounds.left, 
  535.                                 newBounds.bottom-newBounds.top, true);
  536.                 }
  537.             }
  538.         }
  539.         break;
  540.     case pName:
  541.         {
  542.             CStr255        theName;
  543.             err = GetCStringFromDescriptor(theData, &theName);
  544.             if (err == noErr)
  545.             {
  546.                 fName = theName;
  547.                 if (fInitialized && fWindowPtr)
  548.                     SetWTitle(fWindowPtr, (ConstStr255Param) theName);
  549.             }
  550.         }
  551.         break;
  552.     case pVisible:
  553.         Boolean        theBoolean = false;
  554.         err = GetBooleanFromDescriptor(theData, &theBoolean);
  555.         if (err == noErr)
  556.         {
  557.             fShouldBeVisible = theBoolean;
  558.             if (fInitialized)
  559.             {
  560.                 if (fShouldBeVisible)
  561.                     OpenObject();
  562.                 else
  563.                     CloseObject();
  564.             }
  565.         }
  566.         break;
  567.     default:
  568.         err = TScriptableObject::SetProperty(propertyID, theData);
  569.         break;
  570.     }
  571.     
  572.     return err;
  573. }    
  574.  
  575.  
  576.  
  577. OSErr TWindowObj::GetObjectSpecifier  (AEDesc *result)
  578. {
  579.     // walk along containment hierarchy & build object specifier 
  580.     //
  581.     // top container is null container
  582.     // then remainder are the object classes specified by name
  583.     //
  584.     OSErr         err = 0;
  585.     AEDesc        containerDesc, newContainerDesc;
  586.     
  587.     err = MakeNullDesc(&containerDesc);
  588.     if (err == noErr)
  589.     {
  590.         AEDesc        nameDesc;
  591.         
  592.         err = MakeNameDesc(fName, &nameDesc);
  593.         if (err == noErr)
  594.         {
  595.             err = CreateObjSpecifier(cWindow, &containerDesc, formName, 
  596.                                      &nameDesc, true, &newContainerDesc);
  597.             containerDesc = newContainerDesc;
  598.         }
  599.     }
  600.     
  601.     *result = containerDesc;
  602.         
  603.     return err;
  604. }    
  605.  
  606.  
  607. TScriptableObject* TWindowObj::FindElement(Point& thePt, DescType desiredClass)
  608. {
  609.     TListOfLongs    aList;    // makes temporary copy of list
  610.     
  611.     if (desiredClass == cButton)
  612.         aList = fButtonElems;
  613.     else if (desiredClass == cTextLabel)
  614.         aList = fLabelElems;
  615.         
  616.     long numElems = aList.CountElements();
  617.     while (numElems > 0)
  618.     {
  619.         TInterfaceObj*    anElement = (TInterfaceObj*)(aList.GetElement(numElems));
  620.         if (anElement)
  621.         {
  622.             Rect    theBounds;
  623.             
  624.             anElement->GetBounds(theBounds);
  625.             
  626.             if (PtInRect(thePt, &theBounds))
  627.                 return anElement;
  628.         }
  629.         numElems--;
  630.     }
  631.     return NULL;
  632. }
  633.  
  634.  
  635. OSErr TWindowObj::GetTargetObjectSpecifier  (EventRecord& theEvent, AEDesc *result)
  636. {
  637.     OSErr         err = 0;
  638.     AEDesc        containerDesc;
  639.     
  640.     if (theEvent.what == mouseDown)
  641.     {
  642.         Point    clickPt = theEvent.where;
  643.         
  644.         GlobalToLocal(&clickPt);
  645.         
  646.         TInterfaceObj* theElement = (TInterfaceObj*)FindElement(clickPt, cButton);
  647.         if (!theElement)
  648.             theElement = (TInterfaceObj*)FindElement(clickPt, cTextLabel);
  649.             
  650.         if (theElement)
  651.             err = theElement->GetObjectSpecifier(&containerDesc);
  652.         else
  653.             err = GetObjectSpecifier(&containerDesc);
  654.     }
  655.     else
  656.         err = GetObjectSpecifier(&containerDesc);
  657.         
  658.     *result = containerDesc;
  659.         
  660.     return err;
  661. }    
  662.  
  663.  
  664. void TWindowObj::FixUpScriptReferences(TScriptableObject* theParent)
  665. {
  666.     // first call inherited routine in case old script is our parent
  667.     TScriptableObject::FixUpScriptReferences(theParent);
  668.  
  669.     // next, fix up references for any contained objects...
  670.     TInterfaceObj    *anInterfaceObj = NULL;
  671.     
  672.     long numElems = fButtonElems.CountElements();
  673.     while (numElems > 0)
  674.     {
  675.         anInterfaceObj = (TInterfaceObj*)(fButtonElems.GetElement(numElems));
  676.         if (anInterfaceObj)
  677.             anInterfaceObj->FixUpScriptReferences(theParent);
  678.         numElems--;
  679.     }
  680.     
  681.     numElems = fLabelElems.CountElements();
  682.     while (numElems > 0)
  683.     {
  684.         anInterfaceObj = (TInterfaceObj*)(fLabelElems.GetElement(numElems));
  685.         if (anInterfaceObj)
  686.             anInterfaceObj->FixUpScriptReferences(theParent);
  687.         numElems--;
  688.     }
  689. }
  690.  
  691.  
  692.  
  693. /**********************************************************************
  694. ** PUBLIC Constructor
  695. ***********************************************************************/
  696.  
  697. TInterfaceObj::TInterfaceObj(TWindowObj* container)
  698.         : TScriptableObject(container)
  699. {    
  700.     fParentObj = container;
  701.     InitLabels();
  702.     fInitialized = true;
  703. }
  704.  
  705. TInterfaceObj::TInterfaceObj(TWindowObj* container, 
  706.                             const AEDesc *theData)
  707.         : TScriptableObject(container)
  708. {    
  709.     fParentObj = container;
  710.     InitLabels();
  711.     SetData(theData);
  712.     fInitialized = true;
  713. }
  714.  
  715. TInterfaceObj::~TInterfaceObj()
  716. {
  717. }
  718.  
  719. void TInterfaceObj::InitLabels(void)
  720. {
  721.     Str255        fontName;
  722.     
  723.     fInitialized = false;
  724.     fBounds.left = 0;
  725.     fBounds.top = 0;
  726.     fBounds.right = 0;
  727.     fBounds.bottom = 0;
  728.     GetFontName(applFont, fontName);
  729.     fFontName = fontName;
  730.     fName = "";
  731.     fFontSize = 12;
  732.     fFontStyle = 0;
  733.     fFgColor.red = 0xFFFF;
  734.     fFgColor.green = 0xFFFF;
  735.     fFgColor.blue = 0xFFFF;
  736. }
  737.  
  738.  
  739. DescType TInterfaceObj::GetClass(void)
  740. {
  741.     return typeNull;
  742. }
  743.  
  744.  
  745. void TInterfaceObj::DrawObject(void)
  746. {
  747. }
  748.  
  749.  
  750. void TInterfaceObj::ForceRedrawObject(Boolean drawNow)
  751. {
  752.     if (drawNow)
  753.         DrawObject();
  754.     else
  755.     {
  756.         Rect theBounds = fBounds;
  757.         InvalRect(&theBounds);
  758.     }
  759. }
  760.  
  761.  
  762. OSErr TInterfaceObj::ResolveContainer(TScriptableObject **theContainerObj)
  763. {
  764.     OSErr             err = 0;
  765.     
  766.     *theContainerObj = fParentObj;
  767.  
  768.     return err;
  769. }
  770.  
  771.                                     
  772. OSErr TInterfaceObj::GetProperty  (DescType propertyID, DescType wantType, AEDesc *result)
  773. {
  774.     OSErr                 err = errAEEventNotHandled;
  775.     
  776.     switch (propertyID)
  777.     {
  778.     case pBounds:
  779.         {
  780.             Rect theRect = fBounds;
  781.             err = AECreateDesc(typeQDRectangle, (Ptr)&theRect,
  782.                                 sizeof(theRect), result);
  783.         }
  784.         break;
  785.     case pPointSize:
  786.         {
  787.             short theNum = fFontSize;
  788.             err = AECreateDesc(typeShortInteger, (Ptr)&theNum,
  789.                                 sizeof(theNum), result);
  790.         }
  791.         break;
  792.     case pFont:
  793.         {
  794.             CStr255    theName = fFontName;
  795.             err = AECreateDesc(typeChar, (Ptr)&theName[1], 
  796.                                 theName.Length(), result);
  797.         }
  798.         break;
  799.     case pName:
  800.         {
  801.             CStr255    theName = fName;
  802.             err = AECreateDesc(typeChar, (Ptr)&theName[1], 
  803.                                 theName.Length(), result);
  804.         }
  805.         break;
  806.     default:
  807.         err = TScriptableObject::GetProperty(propertyID, wantType, result);
  808.         break;
  809.     }
  810.     
  811.     return err;
  812. }
  813.                                             
  814. OSErr TInterfaceObj::SetProperty  (DescType propertyID, const AEDesc *theData)
  815. {
  816.     OSErr                 err = errAEEventNotHandled;
  817.     
  818.     switch (propertyID)
  819.     {
  820.     case pBounds:
  821.         {
  822.             Rect    newBounds;
  823.             err = GetRectFromDescriptor(theData, &newBounds);
  824.             if (err == noErr)
  825.             {
  826.                 ForceRedrawObject(false);
  827.                 fBounds = newBounds;
  828.                 ForceRedrawObject(false);
  829.             }
  830.         }
  831.         break;
  832.     case pFont:
  833.         {
  834.             CStr255        theName;
  835.             err = GetCStringFromDescriptor(theData, &theName);
  836.             if (err == noErr)
  837.             {
  838.                 fFontName = theName;
  839.                 ForceRedrawObject(false);
  840.             }
  841.         }
  842.         break;
  843.     case pName:
  844.         {
  845.             CStr255        theName;
  846.             err = GetCStringFromDescriptor(theData, &theName);
  847.             if (err == noErr)
  848.             {
  849.                 fName = theName;
  850.                 ForceRedrawObject(false);
  851.             }
  852.         }
  853.         break;
  854.     case pPointSize:
  855.         {
  856.             short        theNum = 0;
  857.             err = GetIntegerFromDescriptor(theData, &theNum);
  858.             if (err == noErr)
  859.             {
  860.                 fFontSize = theNum;
  861.                 ForceRedrawObject(false);
  862.             }
  863.         }
  864.         break;
  865.     default:
  866.         err = TScriptableObject::SetProperty(propertyID, theData);
  867.         break;
  868.     }
  869.     
  870.     return err;
  871. }    
  872.  
  873.  
  874.  
  875.  
  876. OSErr TInterfaceObj::GetObjectSpecifier  (AEDesc *result)
  877. {
  878.     // walk along containment hierarchy & build object specifier 
  879.     //
  880.     // top container is null container
  881.     // then remainder are the object classes specified by name
  882.     //
  883.     OSErr         err = 0;
  884.     AEDesc        containerDesc, newContainerDesc;
  885.     
  886.     err = fParentObj->GetObjectSpecifier(&containerDesc);
  887.     if (err == noErr)
  888.     {
  889.         AEDesc        nameDesc;
  890.         
  891.         err = MakeNameDesc(fName, &nameDesc);
  892.         if (err == noErr)
  893.         {
  894.             err = CreateObjSpecifier(GetClass(), &containerDesc, formName, 
  895.                                      &nameDesc, true, &newContainerDesc);
  896.             containerDesc = newContainerDesc;
  897.         }
  898.     }
  899.     
  900.     *result = containerDesc;
  901.         
  902.     return err;
  903. }    
  904.  
  905.  
  906.  
  907.  
  908. /**********************************************************************
  909. ** PUBLIC Constructor
  910. ***********************************************************************/
  911.  
  912. TLabelObj::TLabelObj(TWindowObj* container) 
  913.             : TInterfaceObj (container)
  914. {    
  915.     InitLabels();
  916. }
  917.  
  918. TLabelObj::TLabelObj(TWindowObj* container,
  919.                 const AEDesc *theData) 
  920.             : TInterfaceObj ( container)
  921. {    
  922.     InitLabels();
  923.     SetData(theData);
  924. }
  925.  
  926. TLabelObj::~TLabelObj()
  927. {
  928.     if (fContents)
  929.         DisposHandle(fContents);
  930. }
  931.  
  932.  
  933. void TLabelObj::InitLabels(void)
  934. {
  935.     fContents = NULL;
  936. }
  937.  
  938.  
  939. DescType TLabelObj::GetClass(void)
  940. {
  941.     return cTextLabel;
  942. }
  943.  
  944.  
  945. void TLabelObj::DrawObject(void)
  946. {
  947.     Handle        datah = fContents;
  948.     
  949.     if (fInitialized && datah)
  950.     {
  951.         Rect        theBounds = fBounds;
  952.         
  953.         InsetRect(&theBounds, 3, 3);    // standard 3pt inset for text Labels
  954.         
  955.         if (!EmptyRect(&theBounds))
  956.         {
  957.             HLock(datah);
  958.             TextBox(StripAddress((Ptr)(*datah)), GetHandleSize(datah), 
  959.                     &theBounds, teJustLeft);
  960.             HUnlock(datah);
  961.         }
  962.     }
  963. }
  964.  
  965.  
  966. void TLabelObj::ForceRedrawObject(Boolean drawNow)
  967. {
  968.     TInterfaceObj::ForceRedrawObject(drawNow);
  969. }
  970.  
  971.  
  972.  
  973. OSErr TLabelObj::GetProperty  (DescType propertyID, DescType wantType, AEDesc *result)
  974. {
  975.     OSErr                 err = errAEEventNotHandled;
  976.     
  977.     switch (propertyID)
  978.     {
  979.     case pContents:
  980.         Handle     h = fContents;
  981.         err = HandToHand(&h);
  982.         if (err == noErr)
  983.         {
  984.             result->descriptorType = typeChar;
  985.             result->dataHandle = h;
  986.         }
  987.         break;
  988.     default:
  989.         err = TInterfaceObj::GetProperty(propertyID, wantType, result);
  990.         break;
  991.     }
  992.     
  993.     return err;
  994. }
  995.                                             
  996. OSErr TLabelObj::SetProperty  (DescType propertyID, const AEDesc *theData)
  997. {
  998.     OSErr                 err = errAEEventNotHandled;
  999.     
  1000.     switch (propertyID)
  1001.     {
  1002.     case pContents:
  1003.         {
  1004.             AEDesc        displayData;
  1005.             OSAID        valueID = kOSANullScript;
  1006.             
  1007.             err = OSACoerceFromDesc(gScriptingComponent, theData,
  1008.                                     kOSAModeNull, &valueID);
  1009.                                     
  1010.             if (err == noErr && valueID != kOSANullScript)
  1011.                 err = OSADisplay(gScriptingComponent, valueID, 
  1012.                                     typeChar, kOSAModeDisplayForHumans, 
  1013.                                     &displayData);
  1014.     
  1015.             OSADispose(gScriptingComponent, valueID);
  1016.             
  1017.             if (err == noErr && displayData.dataHandle)
  1018.             {
  1019.                 Handle     h = displayData.dataHandle;
  1020.                 err = HandToHand(&h);
  1021.                 if (fContents)
  1022.                     DisposHandle(fContents);
  1023.                 fContents = h;
  1024.                 AEDisposeDesc(&displayData);
  1025.                 ForceRedrawObject(false);
  1026.             }
  1027.             else
  1028.                 printf("Failed to set contents of label: err=%ld\n", err);
  1029.         }
  1030.         break;
  1031.     default:
  1032.         err = TInterfaceObj::SetProperty(propertyID, theData);
  1033.         break;
  1034.     }
  1035.     
  1036.     return err;
  1037. }    
  1038.  
  1039.  
  1040. /**********************************************************************
  1041. ** PUBLIC Constructor
  1042. ***********************************************************************/
  1043.  
  1044. TButtonObj::TButtonObj(TWindowObj* container) 
  1045.         : TInterfaceObj (container)
  1046. {    
  1047.     InitLabels();
  1048. }
  1049.  
  1050. TButtonObj::TButtonObj(TWindowObj* container, 
  1051.                         const AEDesc *theData) 
  1052.         : TInterfaceObj (container)
  1053. {    
  1054.     InitLabels();
  1055.     SetData(theData);
  1056. }
  1057.  
  1058. TButtonObj::~TButtonObj()
  1059. {
  1060.     if (fControlH)
  1061.         DisposeControl(fControlH); 
  1062. }
  1063.  
  1064. void TButtonObj::InitLabels(void)
  1065. {
  1066.     TInterfaceObj::InitLabels();
  1067.     
  1068.     fButtonKind = kAEBtnStandard;
  1069.     fControlH = NULL;
  1070. }
  1071.  
  1072.  
  1073. DescType TButtonObj::GetClass(void)
  1074. {
  1075.     return cButton;
  1076. }
  1077.  
  1078.  
  1079. void TButtonObj::DrawObject(void)
  1080. {
  1081.     if (!fControlH)
  1082.     {
  1083.         // create the button
  1084.         Rect        theBounds = fBounds;
  1085.         CStr255        title = fName;
  1086.         short        theProcID = pushButProc;    // assume standard by default
  1087.         WindowPtr    theWindow = ((TWindowObj*)fParentObj)->GetWindowPtr();
  1088.         
  1089.         if (fButtonKind == kAEBtnCheckbox)
  1090.             theProcID = checkBoxProc;
  1091.         else if (fButtonKind == kAEBtnCheckbox)
  1092.             theProcID = radioButProc;
  1093.         
  1094.         fControlH = NewControl(theWindow, &theBounds, title, true,
  1095.                                 0, 0, 1, theProcID, (long)this);
  1096.     }
  1097.     if (fControlH)
  1098.         Draw1Control(fControlH);
  1099. }
  1100.  
  1101.  
  1102. void TButtonObj::ForceRedrawObject(Boolean drawNow)
  1103. {
  1104.     TInterfaceObj::ForceRedrawObject(drawNow);
  1105. }
  1106.  
  1107.  
  1108. OSErr TButtonObj::GetProperty  (DescType propertyID, DescType wantType, AEDesc *result)
  1109. {
  1110.     OSErr                 err = errAEEventNotHandled;
  1111.     
  1112.     switch (propertyID)
  1113.     {
  1114.     case pButtonKind:
  1115.         DescType theKind = fButtonKind;
  1116.         err = AECreateDesc(typeType, (Ptr)&theKind, sizeof(theKind), result);
  1117.         break;
  1118.     default:
  1119.         err = TInterfaceObj::GetProperty(propertyID, wantType, result);
  1120.         break;
  1121.     }
  1122.     
  1123.     return err;
  1124. }
  1125.                                             
  1126. OSErr TButtonObj::SetProperty  (DescType propertyID, const AEDesc *theData)
  1127. {
  1128.     OSErr                 err = errAEEventNotHandled;
  1129.     
  1130.     switch (propertyID)
  1131.     {
  1132.     case pBounds:
  1133.         err = TInterfaceObj::SetProperty(propertyID, theData);
  1134.         if (err == noErr && fControlH)
  1135.         {
  1136.             Rect newBounds = fBounds;
  1137.             MoveControl(fControlH, newBounds.left, newBounds.top);
  1138.             SizeControl(fControlH, newBounds.right-newBounds.left, 
  1139.                         newBounds.bottom-newBounds.top);
  1140.         }
  1141.         break;
  1142.     case pButtonKind:
  1143.         DescType theKind;
  1144.         err = GetDescTypeFromDescriptor(theData, &theKind);
  1145.         if (err == noErr)
  1146.             fButtonKind = theKind;
  1147.         break;
  1148.     default:
  1149.         err = TInterfaceObj::SetProperty(propertyID, theData);
  1150.         break;
  1151.     }
  1152.         
  1153.     return err;
  1154. }    
  1155.